/***************************************************************************
| File: serial.c  -  Comandos de acesso a leitura e escrita RS232
|
| Autor: Joao Carlos Bittencourt, Vinicius Bittencourt, Italo Santos (IST)
| Data:  Maio 2012
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cyg/io/io.h>
#include <cyg/kernel/kapi.h>
#include "../include/constrains.h"

extern Cyg_ErrNo err;
extern cyg_io_handle_t serH;
extern cyg_handle_t mbx1H, mbx2H, mbx3H;
extern int sig_serial;
extern int debug;

unsigned int n;
unsigned int nr; /* number of registers to return */
unsigned char response[MAX];

void serial_write(void)
{
	unsigned char *message;
	int i;

	while(1)
	{
		message = cyg_mbox_get( mbx1H );  // wait for message
		n = sizeof(message);
		n--;
		if(sig_serial == 1)
		{
			err = cyg_io_write( serH, message, &n );
			printf(":::: io_write err=%x, n=%d\n", err, n);
		}
		if(debug)
		{
			printf("\n>> Message sent, NR=%d\n", n);
			for (i = 0; i < n; i++)
				printf(":: bufw[%d]=0x%X\n", i, message[i]);	
		}
	}	
}

void serial_read(void)
{
	char bufr[MAX]; /* read buffer */ 

	while(1)
	{
		if(sig_serial == 1)
		{
			err = cyg_io_read( serH, bufr, &nr );
	  		printf(":::: io_read err=%x, n=%d buf=0x%X\n", err, nr, bufr[0]);
		}
		cyg_mbox_put( mbx2H, bufr );  
	}
	
}
